//
//  informationNotificationTVC.m
//  HappyStore
//
//  Created by 李小辉 on 16/10/20.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "informationNotificationTVC.h"
#import "addressCell.h"
@interface informationNotificationTVC ()
@property (assign, nonatomic) NSInteger page;
@property (strong, nonatomic) NSMutableArray *aryData;
@end

@implementation informationNotificationTVC

- (NSMutableArray *)aryData{
    if (!_aryData) {
        _aryData = [NSMutableArray array];
    }
    return _aryData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消息通知";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:19.0f],NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    //去掉自定义cell底部的黑线
//    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    // 注册
    UINib * nib = [UINib nibWithNibName:@"addressCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"xiaoxinoti"];
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    // 设置刷新图片
    UIImage *images = [UIImage imageNamed:@"mj_footer"];
    NSArray *refreshingImages = @[images];
    [footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多内容了" forState:MJRefreshStateRefreshing];
    footer.stateLabel.font = [UIFont systemFontOfSize:12.0f];
    footer.stateLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0f];
    
    [footer setAutomaticallyRefresh:NO];
    
    self.tableView.mj_footer = footer;
    self.page = 1;
    [self startNetWork];
    //去掉多余的表格线
    [self setExtraCellLineHidden:self.tableView];
}

- (void)setExtraCellLineHidden:(UITableView *)tableView{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}
- (void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)loadMoreData{
    
    self.page++;
    [self startNetWork];
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:self.aryData.count];
    return self.aryData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    addressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xiaoxinoti"];
    
    
    
    NSDictionary *dicData = self.aryData[indexPath.row];
    
    [cell.imageview setImage:[UIImage imageNamed:@"logo"]];
    cell.name.text = dicData[@"content"];
    [cell.name setFont:[UIFont fontWithName:@"HiraKakuProN-W3" size:14]];
    
    NSString *str = [dicData[@"createTime"] substringWithRange:NSMakeRange(10, 6) ];
    
    NSString *str1 = [dicData[@"createTime"] substringToIndex:10];
    cell.phone.text = [NSString stringWithFormat:@"%@  %@",str1,str];
    [cell.phone setFont:[UIFont fontWithName:@"HiraKakuProN-W3" size:12]];
    cell.phone.textColor = [UIColor lightGrayColor];
    
    cell.imageview.layer.masksToBounds = YES;
    cell.imageview.layer.cornerRadius = 5.0f;
    cell.addBtn.hidden = YES;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
                                                                   message:self.aryData[indexPath.row][@"content"]
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {}];
    
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}
#pragma mark - 网络请求
- (void)startNetWork{
    //系统消息http://localhost:8080/lensum/getSystemList_app_social.im.lensum?token=XF2YkxodEFLmVot6svnA&page=1&account=geren123
    NSMutableDictionary *muDic = [NSMutableDictionary dictionary];
    muDic[@"token"] = [USER_DEFAULTS objectForKey:@"token"];
    muDic[@"account"] = [USER_DEFAULTS objectForKey:@"account"];
    muDic[@"page"] = @(self.page);
    [LSHttpTool sendHttpData:@"getSystemList_app_social.im.lensum" params:muDic success:^(id responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        if ([dic[@"result"] integerValue] == 1) {
            if (self.page == 1) {
                self.aryData = nil;
                NSArray *systemDic = [NSArray arrayWithArray:dic[@"data"][@"system"]];
                self.aryData = [NSMutableArray arrayWithArray:dic[@"data"][@"systemPeople"]];
                for (NSDictionary *dick in systemDic) {
                    [self.aryData addObject:dick];
                }
            }else{
                NSArray *systemDic = [NSArray arrayWithArray:dic[@"data"][@"system"]];
                NSMutableArray *popSystem = [NSMutableArray arrayWithArray:dic[@"data"][@"systemPeople"]];
                for (NSDictionary *dick in systemDic) {
                    [popSystem addObject:dick];
                }
                for (NSDictionary *dict in self.aryData) {
                    [popSystem addObject:dict];
                }
                self.aryData = nil;
                self.aryData = popSystem;
            }
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }

    } failure:^(NSError *error) {
       [self showSingleIconBubble:@"网络连接错误"];
    }];
}
@end
